23 #define foreach(x, v) for (typeof (v).begin() x=(v).begin(); x !=(v).end(); ++x)
24 #define For(i, a, b) for (int i=(a); i<(b); ++i)
25 #define D(x) cout << #x " is " << x << endl
27 const long long oo
= 1LL<<62;
33 bool operator < (const subject
&t
) const {
34 if (c
!= t
.c
) return c
< t
.c
;
35 if (a
!= t
.a
) return a
< t
.a
;
42 vector
< subject
> subjects
;
44 map
< pair
<int, pair
<int, long long > >, long long > memo
;
46 map
< pair
<int, pair
<int, long long > >, pair
<int, long long> > choice
;
48 long long f(int k
, int last
, long long exercises
) {
50 //printf("f(k=%d, last=%d, exercises=%lld) = %lld.\n", k, last, exercises, 0LL);
54 pair
<int, pair
<int, long long > > key
= make_pair(k
, make_pair(last
, exercises
));
55 if (memo
.count(key
)) return memo
[key
];
59 long long choose_exercises
= -1;
62 for (int j
= last
+ 1; j
< subjects
.size(); ++j
) {
63 const subject
&cur
= subjects
[j
];
65 assert(cur
.c
>= subjects
[last
].c
);
66 if (cur
.c
== subjects
[last
].c
) continue; // must be strictly increasing
68 long long new_exercises
[2];
70 new_exercises
[0] = exercises
+ K
;
71 new_exercises
[1] = exercises
* K
;
73 for (int p
= 0; p
< 2; ++p
) {
74 if (cur
.a
<= new_exercises
[p
] and new_exercises
[p
] <= cur
.b
) {
75 long long option
= f(k
+ 1, j
, new_exercises
[p
]) + new_exercises
[p
];
78 choose_exercises
= new_exercises
[p
];
84 } else { // can choose whatever number of exercises I want
86 for (long long e
= cur
.a
; e
<= cur
.b
; ++e
) {
87 long long option
= f(k
+ 1, j
, e
) + e
;
97 //if (ans < oo) printf("f(k=%d, last=%d, exercises=%lld) = %lld. Next is %d with %lld extra exercises\n", k, last, exercises, ans, choose_next, choose_exercises);
99 choice
[key
] = make_pair(choose_next
, choose_exercises
);
106 while (cin
>> N
>> m
>> K
) {
109 cin
>> subjects
[i
].a
>> subjects
[i
].b
>> subjects
[i
].c
;
112 sort(subjects
.begin(), subjects
.end());
114 // printf("Subject %d = (a=%lld, b=%lld, c=%d)\n", i, subjects[i].a, subjects[i].b, subjects[i].c);
117 long long ans
= f(0, -1, 0);
119 cout
<< "NO" << endl
;
121 cout
<< "YES" << endl
;
123 int k
= 0, last
= -1; long long e
= 0;
124 for (int i
= 0; i
< N
; ++i
) {
125 pair
<int, pair
<int, long long > > key
= make_pair(k
, make_pair(last
, e
));
126 pair
< int, long long > c
= choice
[key
];
127 cout
<< subjects
[c
.first
].idx
+ 1 << " " << c
.second
<< endl
;